
require 'jquery.dropdown'
require 'jquery-lazyload'

ElementUI = require 'element-ui'
VueImg = require 'vue-img'

Vue = require 'vue'
VueTouch = require('vue-touch/dist/vue-touch.min')
Vue.use(VueTouch, {name: 'v-touch'})
Vue.use(ElementUI)
Vue.use(VueImg, {
  loading: '',
  error: '',
  prefix: '',
  quality: 0
})
FixedMenu = require './components/fixedMenu.vue'
VueAutoComplete = require './components/autocomplete.vue'
ShoppingTips = require './components/shoppingTips.vue'
HomeReserve = require './components/homeReserve.vue'

class App
  constructor: (components, mountedFun, data={}, computed={}, methods={}, mixins=[], watched={}, util, browser_detect) ->

    document.addEventListener('DOMContentLoaded', () ->

      defaultComponents = {
        'autocompleteInput': VueAutoComplete
        'fixedMenu': FixedMenu
        'shoppingTips': ShoppingTips
        'HomeReserve': HomeReserve
      }

      deletedKeys = commonKeys(components, defaultComponents)

      deletedKeys.forEach((ele) ->
        delete components[ele] if components[ele]
        delete defaultComponents[ele] if defaultComponents[ele]
      )

      defaultData = {
        options: []
      }

      defaultMethods = {
        onOptionSelect: () ->

      }

      defaultComputed = {}

      defaultWatched = {}

      unless /^\/product_cases/.test(window.location.pathname)
        $("img").lazyload(
          threshold : 500
          placeholder: "data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20xmlns%3Axlink%3D%27http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%27%20viewBox%3D%270%200%20%7B%7Bw%7D%7D%20%7B%7Bh%7D%7D%27%3E%3Cdefs%3E%3Csymbol%20id%3D%27a%27%20viewBox%3D%270%200%2090%2066%27%20opacity%3D%270.3%27%3E%3Cpath%20d%3D%27M85%205v56H5V5h80m5-5H0v66h90V0z%27%2F%3E%3Ccircle%20cx%3D%2718%27%20cy%3D%2720%27%20r%3D%276%27%2F%3E%3Cpath%20d%3D%27M56%2014L37%2039l-8-6-17%2023h67z%27%2F%3E%3C%2Fsymbol%3E%3C%2Fdefs%3E%3Cuse%20xlink%3Ahref%3D%27%23a%27%20width%3D%2720%25%27%20x%3D%2740%25%27%2F%3E%3C%2Fsvg%3E"
        )

      new Vue(
        el: '#app'
        data: ->
          Vue.util.extend(defaultData, data)
        components: Vue.util.extend(defaultComponents, components)
        methods: Vue.util.extend(defaultMethods, methods)
        computed: Vue.util.extend(defaultComputed, computed)
        watch: Vue.util.extend(defaultWatched, watched)
        mixins: mixins
        mounted: ->
          $('#item-show-more').bind 'click', ->
            $('.inside-menu').toggle 'slow'
          mountedFun.call(this) if mountedFun
      )

    )
  commonKeys = (obj1, obj2) ->
    keys = []
    for i of obj1
      if i of obj2
        keys.push i
    keys

module.exports = (components, mountedFun, data, computed, methods, mixins, watched) ->
                      new App(components, mountedFun, data, computed, methods, mixins, watched)
